package com.xk.meeting.controller;

import com.xk.meeting.entity.User;
import com.xk.meeting.service.UserService;
import com.xk.meeting.service.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/regUser")
public class RegUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        Map<String, String[]> userMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,userMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        UserService userService = new UserServiceImpl();
        int status = userService.reqUser(user);

        if(status != -1){
            response.sendRedirect("login.jsp");
        }else{
            request.setAttribute("msg","注册失败");
            request.getRequestDispatcher("req.jsp").forward(request,response);
        }

    }
}
